﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Net;
using System.Text;
using System.Timers;
using System.Windows.Forms;
using AgentTest;
using DXWinFormDataAnalysis.DataModel;
using DevExpress.XtraCharts;
using DevExpress.XtraEditors;
using GeneralDFS.Client;
using GeneralDFS.Common.Network;
using GeneralDFS.DataEntity.Config;
using GeneralDFS.Interface.Agent;
using NIF.MobileActors.Core;
using NIF.MobileActors.Hosts;

namespace DXWinFormDataAnalysis
{
    public partial class CpuMonitor : DevExpress.XtraEditors.XtraForm
    {
        private SearchNodesActor act;
        private List<NameNodeStatistics> nameNodeStatisticses;
        private System.Timers.Timer autoTimer = new System.Timers.Timer(1);
        private Boolean isAuto = false;
        public CpuMonitor()
        {
            InitializeComponent();
        }

        private void bt_Init_Click(object sender, EventArgs e)
        {
            bt_Init.Enabled = false;
            chart_Line.Series.Clear();
            
            bt_Update.Enabled = true;
            bt_aUpdate.Enabled = true;
        }

        private void bt_Update_Click(object sender, EventArgs e)
        {
            autoTimer.Stop();
            ActorProxy<IEntry> localActor = RemotingHost.LocalFacade.CreateLocalActor<IEntry>(typeof(SearchNodesActor));
            act = localActor.GetActor() as SearchNodesActor;
            Client client = new Client();
            Queue<IPEndPoint> hosts = new Queue<IPEndPoint>();
            foreach (NameNodeConfig nameNodeConfig in client.GetConfig().NameNodeConfigs)
            {
                var endPoint = new IPEndPoint(IPAddress.Parse(nameNodeConfig.Ip), nameNodeConfig.AgentRpcPort);
                hosts.Enqueue(endPoint);
            }

            act.Hosts = hosts;
            act.LocalHost = new IPEndPoint(IPAddress.Parse(IPHelper.GetLocalIP("network")), 8820);
            try
            {

                localActor.Post(h => h.Excute());
                ActorProxy<IEntry> actor = RemotingHost.LocalFacade.GetActorProxy<IEntry>(localActor.ID);
                System.Timers.Timer timer = new System.Timers.Timer(10);
                timer.Elapsed += timer_Elapsed;
                timer.Start();
            }
            catch (Exception exception)
            {
                throw exception;
            }
        }

        void InitDataSource()
        {
            NameNodeStatistics nNS;
            nameNodeStatisticses = new List<NameNodeStatistics>();
            int i = 0;
            while (i < act.NameNodeStatistics.Count)
            {
                nNS = new NameNodeStatistics();
                nNS.ServerName = act.NameNodeStatistics[i].NameNodeInfo.ServerName;
                nNS.Ip = act.NameNodeStatistics[i].NameNodeInfo.Ip;
                nNS.RpcPort = act.NameNodeStatistics[i].NameNodeInfo.RpcPort;
                nNS.AgentRpcPort = act.NameNodeStatistics[i].NameNodeInfo.AgentRpcPort;
                nNS.LastUpdateTime = act.NameNodeStatistics[i].NameNodeInfo.LastUpdateTime;
                nNS.BlockCount = act.NameNodeStatistics[i].BlockCount;
                nNS.DataNodeCount = act.NameNodeStatistics[i].DataNodeCount;
                nNS.StatisticTime = act.NameNodeStatistics[i].StatisticTime;
                nNS.Cpu = act.NameNodeStatistics[i].Cpu;
                nNS.Memory = act.NameNodeStatistics[i].Memory;
                nNS.HardDiskSize = act.NameNodeStatistics[i].HardDiskSize;
                nNS.HardDiskRate = act.NameNodeStatistics[i].HardDiskRate;
                nNS.NodeType = "NameNode";
                i++;
                nameNodeStatisticses.Add(nNS);
            }
        }

        void timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            var timer = (System.Timers.Timer)sender;
            
            if (act.IsLastHost)
            {

                timer.Stop();
                InitDataSource();

                this.Invoke(new MethodInvoker(delegate
                {
                    try
                    {
                        if (chart_Line.Series.Count ==0)
                        {
                            chart_Line.ClearCache();
                            foreach (var statics in nameNodeStatisticses)
                            {
                                var series = new Series(statics.ServerName, ViewType.Line);
                                var point = new SeriesPoint(statics.StatisticTime.ToString("hh-mm:ss"), statics.Cpu);
                                series.Points.Add(point);
                                chart_Line.Series.Add(series);
                            }
                        }
                        else
                        {
                            foreach (var statics in nameNodeStatisticses)
                            {
                                var series = chart_Line.Series[statics.ServerName];
                                var point = new SeriesPoint(statics.StatisticTime.ToString("hh-mm:ss"), statics.Cpu);
                                series.Points.Add(point);
                            }
                        }

                        if (isAuto)
                        {
                            autoTimer.Start();
                        }
                    }
                    catch (Exception ex)
                    {

                        MessageBox.Show(ex.ToString());
                    }
                }));
            }
        }

        private void bt_aUpdate_Click(object sender, EventArgs e)
        {
            isAuto = true;
            bt_aUpdate.Enabled = false;
            autoTimer = new System.Timers.Timer(1);
            autoTimer.Elapsed += delegate
                {
                    bt_Update_Click(sender, e);
                };
            autoTimer.Start();
        }

        private void bt_Stop_Click(object sender, EventArgs e)
        {
            isAuto = false;
            autoTimer.Stop();
            bt_aUpdate.Enabled = true;
        }

        private void bt_clear_Click(object sender, EventArgs e)
        {
            chart_Line.Series.Clear();
        }
    }
}